package com.ychs.lawyer.controller;

import com.github.pagehelper.PageHelper;
import com.ychs.lawyer.bean.Contract;
import com.ychs.lawyer.bean.ContractCancellations;
import com.ychs.lawyer.bean.R;
import com.ychs.lawyer.service.ContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: clh
 * @Date: 2024/07/30/10:18
 * @Description:
 */
@Api(tags = "合同管理")
@RestController
@RequestMapping("/contract")
@Slf4j
public class ContractController {
    @Autowired
    private ContractService contractService;

    @ApiOperation("获取所有合同")
    @GetMapping("/list")
    public R<?> getContractsByCondition(Contract contract) {
        PageHelper.startPage(contract.getPage(),contract.getPageSize());
        List<Contract> contracts = contractService.getContractsByCondition(contract);
        return R.success(contracts);
    }

    @ApiOperation("录入新合同")
    @PostMapping("/add")
    public R<?> addContract(Contract contract){
        int res = contractService.addContract(contract);
         return res > 0?R.success():R.fail();
    }

    @ApiOperation("修改合同")
    @PostMapping ("/edit")
     public R<?> editContract(@RequestBody Contract contract){
        int res = contractService.editContract(contract);
         return res > 0?R.success():R.fail();
    }

    @ApiOperation("修改合同状态")
    @PostMapping("/changeContractActive")
     public R<?> changeContractActive(@RequestBody Contract contract){
        int res = contractService.changeContractActive(contract);
         return res > 0?R.success():R.fail();
    }

    @ApiOperation("注销合同")
    @PostMapping("/delContract")
     public R<?> delContract(@RequestBody Contract contract){
        int res = contractService.delContract(contract);
         return res > 0?R.success():R.fail();
    }

    @ApiOperation("获取已注销的合同")
    @GetMapping("/getCancelContract")
     public R<?> getCancelContract( ContractCancellations contractCancellations){
        PageHelper.startPage(contractCancellations.getPage(),contractCancellations.getPageSize());
       List<ContractCancellations> cancelContracts= contractService.getCancelContract(contractCancellations);
        return R.success(cancelContracts);
    }

    @ApiOperation("重启已注销的合同")
    @PostMapping("/reStart")
     public R<?> reStart(String contractName){
        log.info("contractName:{}",contractName);
        int res = contractService.reStart(contractName);
         return res > 0?R.success():R.fail();
    }

}
